<?php
class Template {
	const MAIN_TEMPLATE = 'layouts/main';
	const DETAIL_TEMPLATE = 'layouts/detail';
	const BLANK_TEMPLATE = 'layouts/blank';
	
	var $template_data = array();
	var $template = SELF::MAIN_TEMPLATE;
	
	function Template() {
		$this->template_data['jsscript'] = '';
		$this->template_data['cssstyle'] = '';
	}

	function set($name, $value) {
		$this->template_data[$name] = $value;
	}

	function set_js($value) {
		$this->template_data['jsscript'] .= "<script type='text/javascript'>".$value."</script>\n";
	}

	function load_js($value) {
		$this->template_data['jsscript'] .= "<script type='text/javascript' src='".base_url().$value."'></script>\n";
	}

	function load_css($value) {
		$this->template_data['cssstyle'] .= "<link rel='stylesheet' href='".base_url().$value."' type='text/css' media='screen'>\n";
	}

	function getMainMenu(){
		$CI =& get_instance();
		$CI->load->model('modules');
		$params = array('parentMenu' => 0,'menuGroup' => 'default');
		$mainMenu = $this->CI->modules->getMenu($params);
		if($mainMenu)
			return $mainMenu;
		else
			return false;
	}
	
	function display($view = '' , $view_data = array(), $return = FALSE) {
		$this->CI =& get_instance();
// 		$CI =& get_instance();
// 		$params = array('parentMenu' => 0,'menuGroup' => 'default');
//		$this->model->modules->getmenu();
// 		$CI->load->database();
// 		$result = $CI->db->query("select * from modules")->result();
		if($this->template == self::MAIN_TEMPLATE)
			$view_data['mainMenu'] = $this->getMainMenu();
		$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));
		return $this->CI->load->view($this->template, $this->template_data, $return);
	}
	
	function setTemplate($template = ''){
		$this->template = $template;
	}
}